/*
 * Copyright (C) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * created on : 2012-07-19
 * author : yanguoyue
 */

#include "napi/native_api.h"
#include <string>
#include <map>

#include "mars/baseevent/active_logic.h"
#include "mars/baseevent/baseprjevent.h"
#include "mars/baseevent/baseevent.h"
#include "mars/comm/xlogger/xlogger.h"
#include "mars/comm/singleton.h"
#include "mars/comm/strutil.h"
#include "mars/comm/bootrun.h"
#include "mars/comm/napi/util/napi_common.h"
#include "mars/comm/napi/util/var_cache.h"
#include "mars/comm/napi/util/comm_function.h"
//#include "mars/log/appender.h"
#include "mars/stn/stn_logic.h"

#include "stn/src/net_core.h"
#include "stn/src/net_source.h"
#include "stn/src/signalling_keeper.h"
#include "stn/config.h"
#include "stn/proto/stnproto_logic.h"
#include "com_ohos_mars_stn_StnLogic_JS2C.h"
#include "hilog/log.h"
#define LOG_DOMAIN 0x0201
#define LOG_TAG "MY_TAG"

using namespace mars::stn;

//DEFINE_FIND_CLASS(KNetJava2C, "com/tencent/mars/stn/StnLogic")

extern "C" {

/*
 * Class:     com_ohos_mars_stn_StnLogic
 * Method:    getLoadLibraries
 * Signature: ()jobject
 */
//JNIEXPORT jobject JNICALL Java_com_ohos_mars_stn_StnLogic_getLoadLibraries(JNIEnv *_env, jclass clz) 
napi_value JS_com_ohos_mars_stn_StnLogic_getLoadLibraries(napi_env env, napi_callback_info info)
{
    return mars::baseevent::getLoadLibraries(env);
}

/*
 * Class:     com_ohos_mars_stn_StnLogic_Java2C
 * Method:    reset
 * Signature: ()V
 */
//DEFINE_FIND_STATIC_METHOD(KJava2C_reset, KNetJava2C, "reset", "()V")
//JNIEXPORT void JNICALL Java_com_ohos_mars_stn_StnLogic_reset(JNIEnv *_env, jclass clz)
napi_value JS_com_ohos_mars_stn_StnLogic_reset(napi_env env, napi_callback_info info)
{
    Reset();
    napi_value result;
    napi_create_int32(env, 0, &result);
    return result;
}

/*
 * Class:     com_ohos_mars_stn_StnLogic_Java2C
 * Method:    resetAndInitEncoderVersion
 * Signature: (I)V
 */
//DEFINE_FIND_STATIC_METHOD(KJava2C_resetAndInitEncoderVersion, KNetJava2C, "resetAndInitEncoderVersion", "(I)V")
//JNIEXPORT void JNICALL Java_com_ohos_mars_stn_StnLogic_resetAndInitEncoderVersion(JNIEnv *_env, jclass clz, jint _packer_encoder_version)
napi_value JS_com_ohos_mars_stn_StnLogic_resetAndInitEncoderVersion(napi_env env, napi_callback_info info)
{
    size_t requireArgc = 1;
    size_t argc = 1;
    napi_value anArg;
    napi_value * args = &anArg;
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));

    napi_valuetype valueType;
    NAPI_CALL(env, napi_typeof(env, anArg, &valueType));
    NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");
    int packer_encoder_version;
    if (napi_ok != napi_get_value_int32(env, anArg, &packer_encoder_version)) {
        napi_create_int32(env, -1, &result);
        return result;
    }
    ResetAndInitEncoderVersion(packer_encoder_version);
    napi_create_int32(env, 0, &result);
    return result;
}


/*
 * Class:     com_ohos_mars_stn_StnLogic_Java2C
 * Method:    setLonglinkSvrAddr
 * Signature: (Ljava/lang/String;[ILjava/lang/String;)V
 */
//DEFINE_FIND_STATIC_METHOD(KJaSva2C_setLonglinkSvrAddr, KNetJava2C, "setLonglinkSvrAddr", "(Ljava/lang/String;[ILjava/lang/String;)V")
//JNIEXPORT void JNICALL Java_com_ohos_mars_stn_StnLogic_setLonglinkSvrAddr(JNIEnv *_env, jclass, jstring _host, jintArray _ports, jstring _debug_ip)
napi_value JS_com_ohos_mars_stn_StnLogic_setLonglinkSvrAddr(napi_env env, napi_callback_info info)
{
    OH_LOG_INFO(LOG_APP, "JS_com_ohos_mars_stn_StnLogic_setLonglinkSvrAddr 001");
    VarCache* cacheInstance = VarCache::Singleton();

    size_t requireArgc = 3;
    size_t argc = 3;
    napi_value * args = new napi_value[3];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));

    if (argc != 3) {
        napi_create_int32(env, -1, &result);
        return result;
    }
    napi_valuetype valueType;
    NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
    NAPI_ASSERT(env, valueType == napi_string, "Wrong argument type. String expected.");

    NAPI_CALL(env, napi_typeof(env, args[1], &valueType));
    NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type. Array expected.");

    NAPI_CALL(env, napi_typeof(env, args[2], &valueType));
    NAPI_ASSERT(env, valueType == napi_string, "Wrong argument type. String expected.");

    std::string host;
    int status = GetNAPIStringValue(env, args[0], host);
    if (status != napi_ok) {
        napi_create_int32(env, -2, &result);
        return result;
    }
    OH_LOG_INFO(LOG_APP, "host1:%s", host.c_str());
    std::string debug_ip;
    status = GetNAPIStringValue(env, args[2], debug_ip);
    if (status != napi_ok) {
        napi_create_int32(env, -3, &result);
        return result;
    }
    OH_LOG_INFO(LOG_APP, "debug_ip1:%s", debug_ip.c_str());

    bool isArray;
    status = napi_is_array(env, args[1], &isArray);
    if (status != napi_ok || !isArray) {
        napi_create_int32(env, -4, &result);
        return result;
    }

    uint32_t arrayLength;
    status = napi_get_array_length(env,
            args[1],
            &arrayLength);
    if (status != napi_ok) {
        napi_create_int32(env, -5, &result);
        return result;
    }
    std::vector < uint16_t > ports;
    int idx;
    for (idx = 0; idx < arrayLength; ++idx) {
        napi_value elementValue;
        status = napi_get_element(env,
                args[1],
                idx,
                &elementValue);
        if (status != napi_ok) {
            napi_create_int32(env, -6, &result);
            return result;
        }
        int aPort;
        if (napi_ok != napi_get_value_int32(env, elementValue, &aPort)) {
            napi_create_int32(env, -1, &result);
            return result;
        }
        OH_LOG_INFO(LOG_APP, "add port1:%d", aPort);
        ports.push_back((uint16_t)(aPort));
    }
    SetLonglinkSvrAddr(host, ports, debug_ip);
    napi_create_int32(env, 0, &result);
    OH_LOG_INFO(LOG_APP, "JS_com_ohos_mars_stn_StnLogic_setLonglinkSvrAddr 002");
    return result;
}

/*
 * Class:     com_ohos_mars_stn_StnLogic_Java2C
 * Method:    setShortlinkSvrAddr
 * Signature: (ILjava/lang/String;)V
 */
//DEFINE_FIND_STATIC_METHOD(KJava2C_setShortlinkSvrAddr, KNetJava2C, "setShortlinkSvrAddr", "(ILjava/lang/String;)V")
//JNIEXPORT void JNICALL Java_com_ohos_mars_stn_StnLogic_setShortlinkSvrAddr(JNIEnv *_env, jclass, jint _port, jstring _debug_ip)
napi_value JS_com_ohos_mars_stn_StnLogic_setShortlinkSvrAddr(napi_env env, napi_callback_info info)
{
    VarCache* cacheInstance = VarCache::Singleton();

    size_t requireArgc = 2;
    size_t argc = 2;
    napi_value * args = new napi_value[2];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));

    if (argc != 2) {
        napi_create_int32(env, -1, &result);
        return result;
    }

    napi_valuetype valueType;
    NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
    NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");

    NAPI_CALL(env, napi_typeof(env, args[1], &valueType));
    NAPI_ASSERT(env, valueType == napi_string, "Wrong argument type. String expected.");

    int port;
    if (napi_ok != napi_get_value_int32(env, args[0], &port)) {
        napi_create_int32(env, -1, &result);
        return result;
    }
    std::string debug_ip;
    int status = GetNAPIStringValue(env, args[1], debug_ip);
    if (status != napi_ok) {
        napi_create_int32(env, -3, &result);
        return result;
    }

    SetShortlinkSvrAddr(port, debug_ip);
    napi_create_int32(env, 0, &result);
    return result;

}

/*
 * Class:     com_ohos_mars_stn_StnLogic_Java2C
 * Method:    setDebugIP
 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
 */
//DEFINE_FIND_STATIC_METHOD(KJava2C_setDebugIP, KNetJava2C, "setDebugIP", "(Ljava/lang/String;Ljava/lang/String;)V")
//JNIEXPORT void JNICALL Java_com_ohos_mars_stn_StnLogic_setDebugIP(JNIEnv *_env, jclass, jstring _host, jstring _debug_ip) 
napi_value JS_com_ohos_mars_stn_StnLogic_setDebugIP(napi_env env, napi_callback_info info)
{
    VarCache* cacheInstance = VarCache::Singleton();
    //napi_env jsenv = cacheInstance->GetJSEnv();

    size_t requireArgc = 2;
    size_t argc = 2;
    napi_value * args = new napi_value[2];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));

    if (argc != 2) {
        napi_create_int32(env, -1, &result);
        return result;
    }

    napi_valuetype valueType;
    NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
    NAPI_ASSERT(env, valueType == napi_string, "Wrong argument type. String expected.");

    NAPI_CALL(env, napi_typeof(env, args[1], &valueType));
    NAPI_ASSERT(env, valueType == napi_string, "Wrong argument type. String expected.");

    std::string host, debug_ip;
    int status = GetNAPIStringValue(env, args[0], host);
    if (status != napi_ok) {
        napi_create_int32(env, -2, &result);
        return result;
    }
    status = GetNAPIStringValue(env, args[1], debug_ip);
    if (status != napi_ok) {
        napi_create_int32(env, -3, &result);
        return result;
    }

    SetDebugIP(host, debug_ip);
    napi_create_int32(env, 0, &result);
    return result;
}

/*
 * Class:     com_ohos_mars_stn_StnLogic_Java2C
 * Method:    setBackupIPs
 * Signature: (Ljava/lang/String;[Ljava/lang/String;)V
 */
//DEFINE_FIND_STATIC_METHOD(KJava2C_setBackupIPs, KNetJava2C, "setBackupIPs", "(Ljava/lang/String;[Ljava/lang/String;)V")
//JNIEXPORT void JNICALL Java_com_ohos_mars_stn_StnLogic_setBackupIPs(JNIEnv *_env, jclass, jstring _host, jobjectArray _objarray)
napi_value JS_com_ohos_mars_stn_StnLogic_setBackupIPs(napi_env env, napi_callback_info info)
{
    VarCache* cacheInstance = VarCache::Singleton();
    //napi_env jsenv = cacheInstance->GetJSEnv();

    size_t requireArgc = 2;
    size_t argc = 2;
    napi_value * args = new napi_value[2];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));

    if (argc != 2) {
        napi_create_int32(env, -1, &result);
        return result;
    }

    napi_valuetype valueType;
    NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
    NAPI_ASSERT(env, valueType == napi_string, "Wrong argument type. String expected.");

    NAPI_CALL(env, napi_typeof(env, args[1], &valueType));
    NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type. Array expected.");

    std::string host;
    int status = GetNAPIStringValue(env, args[0], host);
    if (status != napi_ok) {
        napi_create_int32(env, -2, &result);
        return result;
    }

    bool isArray;
    status = napi_is_array(env, args[1], &isArray);
    if (status != napi_ok || !isArray) {
        napi_create_int32(env, -3, &result);
        return result;
    }

    uint32_t arrayLength;
    status = napi_get_array_length(env,
            args[1],
            &arrayLength);
    if (status != napi_ok) {
        napi_create_int32(env, -5, &result);
        return result;
    }

    std::vector < std::string > backupip_list;
    int idx;
    for (idx = 0; idx < arrayLength; ++idx) {
        napi_value elementValue;
        status = napi_get_element(env,
                args[1],
                idx,
                &elementValue);
        if (status != napi_ok) {
            napi_create_int32(env, -6, &result);
            return result;
        }
        std::string ip;
        if (napi_ok != GetNAPIStringValue(env, elementValue, ip)) {
            napi_create_int32(env, -6, &result);
            return result;
        }
        backupip_list.push_back(ip);
    }

    SetBackupIPs(host, backupip_list);
    napi_create_int32(env, 0, &result);
    return result;
}

/*
 * Class:     com_ohos_mars_stn_StnLogic_Java2C
 * Method:    startTask
 * Signature: (Lcom/tencent/mars/stn/StnLogic$Task;)V
 */
//DEFINE_FIND_STATIC_METHOD(KJava2C_startTask, KNetJava2C, "startTask", "(Lcom/tencent/mars/stn/StnLogic$Task;)V")
//JNIEXPORT void JNICALL Java_com_ohos_mars_stn_StnLogic_startTask(JNIEnv *_env, jclass, jobject _task)
napi_value JS_com_ohos_mars_stn_StnLogic_startTask(napi_env env, napi_callback_info info)
{
    xverbose_function();
    VarCache* cacheInstance = VarCache::Singleton();
    //napi_env jsenv = cacheInstance->GetJSEnv();

    size_t requireArgc = 1;
    size_t argc = 1;
    napi_value taskValue;
    napi_value * args = &taskValue;
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));

    if (argc != 1) {
        napi_create_int32(env, -1, &result);
        return result;
    }

    std::map < std::string, napi_value > property_map;
    int status = VarCache::FindProperties(taskValue, property_map);

    int taskid;
    status = JS_GetProperty(env, taskValue, "taskID", &taskid);
    if (status != napi_ok) {
        napi_create_int32(env, -1, &result);
        return result;
    }

    //init struct Task
    struct Task task(taskid);

    int cmdid = 0;
    status = JS_GetProperty(env, taskValue, "cmdID", &cmdid);
    if (status != napi_ok) {
        napi_create_int32(env, -1, &result);
        return result;
    }

    int channel_select = 0;
    status = JS_GetProperty(env, taskValue, "channelSelect", &channel_select);
    if (status != napi_ok) {
        napi_create_int32(env, -1, &result);
        return result;
    }

    bool isArray = false;
    napi_value hostlist = JS_GetProperty(env, taskValue, "shortLinkHostList");
    status = napi_is_array(env, hostlist, &isArray);
    if (status != napi_ok || !isArray) {
        napi_create_int32(env, -3, &result);
        return result;
    }

    uint32_t arrayLength = 0;
    status = napi_get_array_length(env,
            hostlist,
            &arrayLength);
    if (status != napi_ok) {
        napi_create_int32(env, -5, &result);
        return result;
    }

    int idx;
    for (idx = 0; idx < arrayLength; ++idx) {
        napi_value elementValue;
        status = napi_get_element(env,
                hostlist,
                idx,
                &elementValue);
        if (status != napi_ok) {
            napi_create_int32(env, -6, &result);
            return result;
        }
        std::string host;
        if (napi_ok != GetNAPIStringValue(env, elementValue, host)) {
            napi_create_int32(env, -6, &result);
            return result;
        }
        task.shortlink_host_list.push_back(host);
    }

    std::string cgi;
    status = JS_GetProperty(env, taskValue, "cgi", &cgi);
    if (napi_ok != status) {
        napi_create_int32(env, -5, &result);
        return result;
    }

    bool send_only = false;
    status = JS_GetProperty(env, taskValue, "sendOnly", &send_only);

    bool need_authed = false;
    status = JS_GetProperty(env, taskValue, "needAuthed", &need_authed);


    napi_value & limit_flow_value = property_map[""];
    bool limit_flow = false;
    status = JS_GetProperty(env, taskValue, "limitFlow", &limit_flow);


    bool limit_frequency = false;
    status = JS_GetProperty(env, taskValue, "limitFrequency", &limit_frequency);

    int channel_strategy = 1;
    status = JS_GetProperty(env, taskValue, "channelStrategy", &channel_strategy);

    bool network_status_sensitive = false;
    status = JS_GetProperty(env, taskValue, "networkStatusSensitive", &network_status_sensitive);

    int priority = 1;
    status = JS_GetProperty(env, taskValue, "priority", &priority);

    int retrycount = 1;
    status = JS_GetProperty(env, taskValue, "retryCount", &retrycount);

    int server_process_cost;
    status = JS_GetProperty(env, taskValue, "serverProcessCost", &server_process_cost);

    int total_timetout = 500;
    status = JS_GetProperty(env, taskValue, "totalTimeout", &total_timetout);

    std::string report_arg;
    status = JS_GetProperty(env, taskValue, "reportArg", &report_arg);

    bool long_polling;
    status = JS_GetProperty(env, taskValue, "longPolling", &long_polling);

    int long_polling_timeout;
    status = JS_GetProperty(env, taskValue, "longPollingTimeout", &long_polling_timeout);

    napi_value headers_value = JS_GetProperty(env, taskValue, "headers");
    std::map < std::string, std::string > headers;
    JS_Object2Map(env, headers_value, headers);

    task.cmdid = cmdid;
    task.channel_select = channel_select;

    task.send_only = send_only;
    task.need_authed = need_authed;
    task.limit_flow = limit_flow;
    task.limit_frequency = limit_frequency;

    task.channel_strategy = channel_strategy;
    task.network_status_sensitive = network_status_sensitive;
    task.priority = priority;

    task.retry_count = retrycount;
    task.server_process_cost = server_process_cost;
    task.total_timeout = total_timetout;
    task.headers = headers;

    task.long_polling = long_polling;
    task.long_polling_timeout = long_polling_timeout;

    if (0 != report_arg.size()) {
        task.report_arg = report_arg;
    }

    if (0 != cgi.size()) {
        task.cgi = cgi;
    }

    napi_value _user_context_value = JS_GetProperty(env, taskValue, "userContext");
    if (NULL != _user_context_value) {
        task.user_context = _user_context_value;
    }

    StartTask(task);
    napi_create_int32(env, 0, &result);
    return result;
}

/*
 * Class:     com_ohos_mars_stn_StnLogic_Java2C
 * Method:    stopTask
 * Signature: (I)V
 */
//DEFINE_FIND_STATIC_METHOD(KJava2C_stopTask, KNetJava2C, "stopTask", "(I)V")
//JNIEXPORT void JNICALL Java_com_ohos_mars_stn_StnLogic_stopTask(JNIEnv *_env, jclass, jint _taskid)
napi_value JS_com_ohos_mars_stn_StnLogic_stopTask(napi_env env, napi_callback_info info)
{
    xverbose_function();

    VarCache* cacheInstance = VarCache::Singleton();
    //napi_env jsenv = cacheInstance->GetJSEnv();

    size_t requireArgc = 1;
    size_t argc = 1;
    napi_value anArg;
    napi_value * args = &anArg;
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));

    if (argc != 1) {
        napi_create_int32(env, -1, &result);
        return result;
    }

    napi_valuetype valueType;
    NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
    NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");

    int taskid;
    if (napi_ok != napi_get_value_int32(env, args[0], &taskid)) {
        napi_create_int32(env, -1, &result);
        return result;
    }

    StopTask(taskid);
    napi_create_int32(env, 0, &result);
    return result;
}

//DEFINE_FIND_STATIC_METHOD(KJava2C_hasTask, KNetJava2C, "hasTask", "(I)Z")
//JNIEXPORT jboolean JNICALL Java_com_ohos_mars_stn_StnLogic_hasTask(JNIEnv *_env, jclass, jint _taskid)
napi_value JS_com_ohos_mars_stn_StnLogic_hasTask(napi_env env, napi_callback_info info)
{
    xverbose_function();

    //napi_env env = cacheInstance->GetJSEnv();

    size_t argc = 1;
    napi_value anArg;
    napi_value * args = &anArg;
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
    int32_t ret = 0;
    if (argc != 1) {
        napi_create_int32(env, ret, &result);
        return result;
    }

    napi_valuetype valueType;
    NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
    NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");

    int taskid;
    if (napi_ok != napi_get_value_int32(env, args[0], &taskid)) {
        napi_create_int32(env, ret, &result);
        return result;
    }

    ret = HasTask(taskid);
    napi_create_int32(env, ret, &result);
    return result;
}

/*
 * Class:     com_ohos_mars_stn_StnLogic_Java2C
 * Method:    redoTask
 * Signature: ()V
 */
//DEFINE_FIND_STATIC_METHOD(KJava2C_redoTask, KNetJava2C, "redoTask", "()V")
//JNIEXPORT void JNICALL Java_com_ohos_mars_stn_StnLogic_redoTask(JNIEnv *_env, jclass)
napi_value JS_com_ohos_mars_stn_StnLogic_redoTask(napi_env env, napi_callback_info info)
{
    xverbose_function();

    size_t requireArgc = 0;
    size_t argc = 1;
    napi_value anArg;
    napi_value * args = &anArg;
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));

    if (argc != 0) {
        napi_create_int32(env, -1, &result);
        return result;
    }

    RedoTasks();
}
/*
 * Class:     com_ohos_mars_stn_StnLogic_Java2C
 * Method:    clearTask
 * Signature: ()V
 */
//DEFINE_FIND_STATIC_METHOD(KJava2C_clearTask, KNetJava2C, "clearTask", "()V")
//JNIEXPORT void JNICALL Java_com_ohos_mars_stn_StnLogic_clearTask(JNIEnv *_env, jclass)
napi_value JS_com_ohos_mars_stn_StnLogic_clearTask(napi_env env, napi_callback_info info)
{
    xverbose_function();

    size_t requireArgc = 0;
    size_t argc = 1;
    napi_value anArg;
    napi_value * args = &anArg;
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));

    if (argc != 0) {
        napi_create_int32(env, -1, &result);
        return result;
    }
    ClearTasks();
}

/*
 * Class:     com_ohos_mars_stn_StnLogic_Java2C
 * Method:    makesureLongLinkConnected
 * Signature: ()V
 */
//JNIEXPORT void JNICALL Java_com_ohos_mars_stn_StnLogic_makesureLongLinkConnected(JNIEnv *_env, jclass)
napi_value JS_com_ohos_mars_stn_StnLogic_makesureLongLinkConnected(napi_env env, napi_callback_info info)
{
    xverbose_function();

    size_t requireArgc = 0;
    size_t argc = 1;
    napi_value anArg;
    napi_value * args = &anArg;
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));

    if (argc != 0) {
        napi_create_int32(env, -1, &result);
        return result;
    }
    MakesureLonglinkConnected();
    napi_create_int32(env, 0, &result);
    return result;
}

/*
 * Class:     com_ohos_mars_stn_StnLogic_Java2C
 * Method:    setSignallingStrategy
 * Signature: (JJ)V
 */
//JNIEXPORT void JNICALL Java_com_ohos_mars_stn_StnLogic_setSignallingStrategy(JNIEnv *_env, jclass, jlong _period, jlong _keep_time)
napi_value JS_com_ohos_mars_stn_StnLogic_setSignallingStrategy(napi_env env, napi_callback_info info)
{
    xverbose_function();

    size_t requireArgc = 2;
    size_t argc = 2;
    napi_value anArg;
    napi_value * args = new napi_value[2];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));

    if (argc != 2) {
        napi_create_int32(env, -1, &result);
        return result;
    }
    int64_t period;
    if (napi_ok != napi_get_value_int64(env, args[0], &period)) {
        napi_create_int32(env, -1, &result);
        return result;
    }
    int64_t keep_time;
    if (napi_ok != napi_get_value_int64(env, args[1], &keep_time)) {
        napi_create_int32(env, -1, &result);
        return result;
    }
    SetSignallingStrategy((long)period, (long)keep_time);
    delete[] args;
    napi_create_int32(env, 0, &result);
    return result;
}

/*
 * Class:     com_ohos_mars_stn_StnLogic_Java2C
 * Method:    keepSignalling
 * Signature: ()V
 */
//JNIEXPORT void JNICALL Java_com_ohos_mars_stn_StnLogic_keepSignalling(JNIEnv *_env, jclass)
napi_value JS_com_ohos_mars_stn_StnLogic_keepSignalling(napi_env env, napi_callback_info info)
{
    xverbose_function();

    size_t requireArgc = 0;
    size_t argc = 1;
    napi_value anArg;
    napi_value * args = &anArg;
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));

    if (argc != 0) {
        napi_create_int32(env, -1, &result);
        return result;
    }
    KeepSignalling();
    napi_create_int32(env, 0, &result);
    return result;
}

/*
 * Class:     com_ohos_mars_stn_StnLogic_Java2C
 * Method:    stopSignalling
 * Signature: ()V
 */
//JNIEXPORT void JNICALL Java_com_ohos_mars_stn_StnLogic_stopSignalling(JNIEnv *_env, jclass)
napi_value JS_com_ohos_mars_stn_StnLogic_stopSignalling(napi_env env, napi_callback_info info)
{
    xverbose_function();

    size_t requireArgc = 0;
    size_t argc = 1;
    napi_value anArg;
    napi_value * args = &anArg;
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));

    if (argc != 0) {
        napi_create_int32(env, -1, &result);
        return result;
    }

    StopSignalling();
    napi_create_int32(env, 0, &result);
    return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_stn_StnLogic_setClientVersion(JNIEnv *_env, jclass, jint _client_version)
napi_value JS_com_ohos_mars_stn_StnLogic_setClientVersion(napi_env env, napi_callback_info info)
{
    size_t requireArgc = 1;
    size_t argc = 1;
    napi_value anArg;
    napi_value * args = &anArg;
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));

    if (argc != 1) {
        napi_create_int32(env, -1, &result);
        return result;
    }

    napi_valuetype valueType;
    NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
    NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");

    int32_t client_version;
    if (napi_ok != napi_get_value_int32(env, args[0], &client_version)) {
        napi_create_int32(env, -1, &result);
        return result;
    }

    mars::stn::SetClientVersion (client_version);
    napi_create_int32(env, 0, &result);
    return result;
}

//JNIEXPORT jint JNICALL Java_com_ohos_mars_stn_StnLogic_genTaskID(JNIEnv *_env, jclass)
napi_value JS_com_ohos_mars_stn_StnLogic_genTaskID(napi_env env, napi_callback_info info)
{
    size_t requireArgc = 0;
    size_t argc = 1;
    napi_value anArg;
    napi_value * args = &anArg;
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));

    if (argc != 0) {
        napi_create_int32(env, -1, &result);
        return result;
    }

    napi_create_int32(env, mars::stn::GenTaskID(), &result);
    return result;
}
}

void ExportSTN() {

}

